<!DOCTYPE html>
<html>
   <head>
      <script type="text/javascript">
         var sock = null;
         var ellog = null;

         window.onload = function() {

            ellog = document.getElementById('log');

            var wsuri;
            if (window.location.protocol === "file:") {
               wsuri = "ws://localhost:9000";
            } else {
               wsuri = "ws://" + window.location.hostname + ":9000";
            }
            //wsuri += "?compressed=true"; // AutobahnPython allows activation of permessage-deflate based on HTTP request parameters

            if ("WebSocket" in window) {
               sock = new WebSocket(wsuri);
            } else if ("MozWebSocket" in window) {
               sock = new MozWebSocket(wsuri);
            } else {
               log("Browser does not support WebSocket!");
            }

            if (sock) {
               sock.onopen = function() {
                  log("Connected to " + wsuri);
                  log("Using WebSocket extensions: " + sock.extensions);
               }

               sock.onclose = function(e) {
                  log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
                  sock = null;
               }

               sock.onmessage = function(e) {
                  log("Got echo: '" + e.data + "' (length " + e.data.length + ")");
               }
            }
         };

         function rstring() {
            return Math.random().toString(36);
         }

         function send() {
            var msg = document.getElementById('message').value;
            for (var i = 0; i < 0; ++i) {
               msg += rstring();
            }
            log("Sending: '" + msg + "' (length " + msg.length + ")");
            if (sock) {
               sock.send(msg);
               log("Sent: " + msg);
            } else {
               log("Not connected.");
            }
         };

         function log(m) {
            ellog.innerHTML += m + '\n';
            ellog.scrollTop = ellog.scrollHeight;
         };
      </script>
   </head>
   <body>
      <h1>Autobahn WebSocket Echo Test</h1>
      <noscript>You must enable JavaScript</noscript>
      <form>
         <p>Message: <input id="message" type="text" size="80" maxlength="1000" value="Hello"></p>
      </form>
      <button onclick='send();'>Send Message</button>
      <pre id="log" style="height: 20em; overflow-y: scroll; background-color: #faa;"></pre>
   </body>
</html>
